package com.lineage.server.datatables;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.L1UltimateBattle;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;

/**
 * 無線大賽設置資料
 *
 * @author dexc
 *
 */
public class UBTable {

	private static final Log _log = LogFactory.getLog(UBTable.class);

	private static UBTable _instance;

	private static final Map<Integer, L1UltimateBattle> _ub = new HashMap<Integer, L1UltimateBattle>();

	public static UBTable getInstance() {
		if (_instance == null) {
			_instance = new UBTable();
		}
		return _instance;
	}

	public void load() {
		final PerformanceTimer timer = new PerformanceTimer();
		java.sql.Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM `ub_settings`");
			rs = pstm.executeQuery();

			while (rs.next()) {

				final L1UltimateBattle ub = new L1UltimateBattle();
				ub.setUbId(rs.getInt("ub_id"));
				ub.setMapId(rs.getShort("ub_mapid"));
				ub.setLocX1(rs.getInt("ub_area_x1"));
				ub.setLocY1(rs.getInt("ub_area_y1"));
				ub.setLocX2(rs.getInt("ub_area_x2"));
				ub.setLocY2(rs.getInt("ub_area_y2"));
				ub.setMinLevel(rs.getInt("min_lvl"));
				ub.setMaxLevel(rs.getInt("max_lvl"));
				ub.setMaxPlayer(rs.getInt("max_player"));
				ub.setEnterRoyal(rs.getBoolean("enter_royal"));
				ub.setEnterKnight(rs.getBoolean("enter_knight"));
				ub.setEnterMage(rs.getBoolean("enter_mage"));
				ub.setEnterElf(rs.getBoolean("enter_elf"));
				ub.setEnterDarkelf(rs.getBoolean("enter_darkelf"));
				ub.setEnterDragonKnight(rs.getBoolean("enter_dragonknight"));
				ub.setEnterIllusionist(rs.getBoolean("enter_illusionist"));
				ub.setEnterMale(rs.getBoolean("enter_male"));
				ub.setEnterFemale(rs.getBoolean("enter_female"));
				ub.setUsePot(rs.getBoolean("use_pot"));
				ub.setHpr(rs.getInt("hpr_bonus"));
				ub.setMpr(rs.getInt("mpr_bonus"));
				ub.resetLoc();

				_ub.put(ub.getUbId(), ub);
			}
			
		} catch (final SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
			
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
		}

		// ub_managers load
		try {
			pstm = con.prepareStatement("SELECT * FROM `ub_managers`");
			rs = pstm.executeQuery();

			while (rs.next()) {
				final L1UltimateBattle ub = this.getUb(rs.getInt("ub_id"));
				if (ub != null) {
					ub.addManager(rs.getInt("ub_manager_npc_id"));
				}
			}
			
		} catch (final SQLException e) {
			_log.error(e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
		}

		// ub_times load
		try {
			pstm = con.prepareStatement("SELECT * FROM `ub_times`");
			rs = pstm.executeQuery();

			while (rs.next()) {
				final L1UltimateBattle ub = this.getUb(rs.getInt("ub_id"));
				if (ub != null) {
					ub.addUbTime(rs.getInt("ub_time"));
				}
			}
			
		} catch (final SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
			
		} finally {
			SQLUtil.close(rs, pstm, con);
		}
		_log.info("載入無線大賽設置資料數量: " + _ub.size() + "(" + timer.get() + "ms)");
	}

	public L1UltimateBattle getUb(final int ubId) {
		return _ub.get(ubId);
	}

	public Collection<L1UltimateBattle> getAllUb() {
		return Collections.unmodifiableCollection(_ub.values());
	}

	public L1UltimateBattle getUbForNpcId(final int npcId) {
		for (final L1UltimateBattle ub : _ub.values()) {
			if (ub.containsManager(npcId)) {
				return ub;
			}
		}
		return null;
	}

	/**
	 * 指定されたUBIDに対するパターンの最大数を返す。
	 *
	 * @param ubId
	 *            調べるUBID。
	 * @return パターンの最大数。
	 */
	public int getMaxPattern(final int ubId) {
		int n = 0;
		java.sql.Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con
			.prepareStatement("SELECT MAX(pattern) FROM `spawnlist_ub` WHERE `ub_id`=?");
			pstm.setInt(1, ubId);
			rs = pstm.executeQuery();
			if (rs.next()) {
				n = rs.getInt(1);
			}
		} catch (final SQLException e) {
			_log.error(e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return n;
	}

}
